package com.wtx.security.config;

import com.wtx.security.service.SysUserService;
import com.wtx.security.util.PermissionUtil;
import com.wtx.security.vo.SysUserVO;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class UserDetailsServiceImpl implements UserDetailsService {

    @Resource
    private SysUserService sysUserService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        SysUserVO sysUserVO = sysUserService.getByUsername(username);
        if(null == sysUserVO){
            throw new RuntimeException("用户不存在");
        }
        List<String> permissionList = sysUserService.getPermissionByUserId(sysUserVO.getId());
        sysUserVO.setPermissionList(permissionList);
        return new MyUser(sysUserVO, PermissionUtil.getAuthorities(permissionList));
    }
}
